package pct.search;

import java.util.LinkedList;
import java.util.Map;
import java.util.Vector;

import org.apache.struts2.interceptor.SessionAware;

import pct.database.PcServiceImpl;
import pct.entity.Group;
import pct.entity.Post;
import pct.entity.User;
import pct.manage.StatusManager;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

public class FindDisease extends ActionSupport implements Preparable, SessionAware {
	private String target;
	private PcServiceImpl pcService = new PcServiceImpl();
	private Map<String, Object> session;
	
	public String execute() {
		StatusManager sm = (StatusManager) session.get("status");
		if(sm == null) {
			sm = new StatusManager();
			User visitor = new User();
			visitor.setUsername("visitor");
			sm.setCurrentUser(visitor);
			session.put("status", sm);
		}
		int length=pcService.getGroupnumber();
		String[] symptom=new String[30];
		symptom=getTarget().split(" ");
		int[] count=new int[length];
		for(int i=0;i<symptom.length;i++){
			Vector temp = pcService.getMatchedDisease(symptom[i]);
			for(int j=0;j<temp.size();j++){
				count[(int) temp.get(j)-1]++;
			}
		}
		int max=count[0];
		int first=0;
		int second=0;
		int third=0;
		int fourth=0;
		int fifth=0;
		for(int i=0;i<count.length;i++){
			if(max<count[i]){
				first=i;
				max=count[i];
			}
		}
		count[first]=0;
		if(max==0){
			first=-1;
			max=count[0];
		}
		else
			max=count[0];
		for(int i=0;i<count.length;i++){
			if(max<count[i]){
				second=i;
				max=count[i];
			}
		}
		count[second]=0;
		if(max==0){
			second=-1;
			max=count[0];
		}
		else
			max=count[0];
		for(int i=0;i<count.length;i++){
			if(max<count[i]){
				third=i;
				max=count[i];
			}
		}
		count[third]=0;
		if(max==0){
			third=-1;
			max=count[0];
		}
		else
			max=count[0];
		for(int i=0;i<count.length;i++){
			if(max<count[i]){
				fourth=i;
				max=count[i];
			}
		}
		count[fourth]=0;
		if(max==0){
			fourth=-1;
			max=count[0];
		}
		else
			max=count[0];
		for(int i=0;i<count.length;i++){
			if(max<count[i]){
				fifth=i;
				max=count[i];
			}
		}
		count[fifth]=0;
		if(max==0){
			fifth=-1;
			max=count[0];
		}
		else
			max=count[0];
		sm.setPageType(7);
		LinkedList<Group> ret=new LinkedList();
		if(first!=-1)
			pcService.getGroup(first,ret);
		if(second!=-1)
			pcService.getGroup(second,ret);
		if(third!=-1)
			pcService.getGroup(third,ret);
		if(fourth!=-1)
			pcService.getGroup(fourth,ret);
		if(fifth!=-1)
			pcService.getGroup(fifth,ret);
		sm.setSearchedDisease(ret);
		session.put("status", sm);
		return SUCCESS;
	}
	
	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	@Override
	public void prepare() throws Exception {
		pcService.connectDB();
	}
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
}